var app = getApp();
const api = require('../../APIConfig/apiConfig.js');
const wxHttpHandler = require('../../utils/wxHttpHandler.js');

Page({
  data: {
    name: '', //姓名
    idCard: '', //身份证
    phone: '', //电话
    title: '', //标题
    content: '', //内容
    imgs: [], //本地图片
    images: [], //上传后的图片
  },

  onLoad: function (options) {
    this.setData({
      userType: app.globalData.userType
    })
  },
  onShow: function () {
    this.setData({
      userType: app.globalData.userType
    })
  },

  //关心日祝福
  toFocuseDay: function (e) {
    wx.navigateTo({
      url: '../focusDayBlessing/focusDayBlessing',
    })
  },
  //今日应到
  toToday: function (e) {
    wx.navigateTo({
      url: '../shouldArriveToday/shouldArriveToday',
    })
  },
  //明日日应到
  toTomorrow: function (e) {
    wx.navigateTo({
      url: '../shouldArriveTomorrow/shouldArriveTomorrow',
    })
  },
  //预约提醒
  toAppointment: function (e) {
    wx.navigateTo({
      url: '../AppointmentReminder/AppointmentReminder',
    })
  },
  //流量池预约
  toFlowCellAppointment: function (e) {
    wx.navigateTo({
      url: '../FlowCellAppointment/FlowCellAppointment',
    })
  },
  //数据汇报
  toDataReport: function (e) {
    wx.navigateTo({
      url: '../DataReport/DataReport',
    })
  },
  //预约登记
  toAppointCheckIn: function (e) {
    wx.navigateTo({
      url: '../AppointCheckIn/AppointCheckIn',
    })
  },
  //新客回访
  toNewGuestReturn: function (e) {
    wx.navigateTo({
      url: '../NewGuestReturn/NewGuestReturn',
    })
  },
  //选择图片
  chooseImg: function (e) {

    if (this.data.imgs.length >= 4) {
      wx.showToast({
        title: '最多上传4张图片',
      })
      return
    }
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        // let imgTem = that.data.imgs
        // imgTem.push(res.tempFilePaths[0])
        // that.setData({
        //   imgs: imgTem
        // })

        console.log(res.tempFilePaths[0])
        wx.uploadFile({
          filePath: res.tempFilePaths[0],
          name: 'file',
          url: api.uploadImg,
          success(res) {
            let imgTem = that.data.imgs
            let result = JSON.parse(res.data)
            console.log(result.data)
            imgTem.push(result.data)
            that.setData({
              imgs: imgTem
            })
          }
        })
      }
    })
  },
  

  //姓名
  nameInput: function (e) {
    this.setData({
      name: e.detail.value
    })
  },

  //身份证
  idCardInput: function (e) {
    this.setData({
      idCard: e.detail.value
    })
  },

  //电话
  phoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  //标题
  titleInput: function (e) {
    this.setData({
      title: e.detail.value
    })
  },

  //内容
  contentInput: function (e) {
    this.setData({
      content: e.detail.value
    })
  },

  //提交
  submitTap: function () {
    if (this.data.name == '') {
      wx.showToast({
        title: '请输入投诉人姓名',
      })
      return
    }

    if (this.data.idCard == '') {
      wx.showToast({
        title: '请输入身份证号码',
      })
      return
    }

    if (this.data.phone == '') {
      wx.showToast({
        title: '请输入电话号码',
      })
      return
    }

    if (this.data.title == '') {
      wx.showToast({
        title: '请输入问题',
      })
      return
    }

    if (this.data.content == '') {
      wx.showToast({
        title: '请填写投诉内容',
      })
      return
    }

    if (this.data.imgs.length == 0) {
      wx.showToast({
        title: '请添加图片',
      })
      return
    }

    let complaint ={
      'phone':this.data.phone,
      'problem':this.data.title,
      'idCard':this.data.idCard,
      'content':this.data.content,
      'complainant':this.data.name,
      'images':this.data.imgs
    }

    wx.showModal({
      title:'提示',
      content:'是否确定提交？',
      showCancel:'true',
      cancelText:'取消',
      confirmText:'提交',
      success(res){
        if(res.confirm){
          // let data = {
          //   'complaint':complaint
          // }
          const that = this
          wxHttpHandler({
            type: 'post',
            path: api.createBusComplaint,
            data: complaint,
            fn: function (res) {
              if (res.code == 0) {
                wx.showToast({
                  title: '已提交',
                })
                wx.navigateBack()
              } else {
                wx.showToast({
                  title: res.desc,
                  icon: 'none',
                  duration: 1500,
                  mask: true
                });
              }
            }
          })
        }
      }
    })
  }



})